/** 
 * @file Load the DataTables plugin on the HTML tables marked with the class bxtr-autoload-datatables
 */
/*
 * This file is part of the BooXtore Student project.
 *
 * (c) Francis Backeland f.backeland@gmail.com
 * (c) Laurine Chaplot chaplot.laurine@orange.fr
 * (c) Julien Dutot julien.dutot@gmail.com
 * (c) Romain Guillaume romain-guillaume@orange.fr
 * (c) Guillaume Paringaux g.paringaux@gmail.com
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code
 */
/** 
 * @namespace 
 */
var forms = {
    datatables: []
};
var autoLoadTables;
/* Create an array with the values of all the checkboxes in a column */
$.fn.dataTable.ext.order['dom-checkbox'] = function  ( settings, col )
{
    return this.api().column( col, {order:'index'} ).nodes().map( function ( td, i ) {
        return $('input', td).prop('checked') ? '1' : '0';
    } );
}
jQuery(document).ready(function() {
    autoLoadTables = function(domSearch, domParent, datatablesOptions) {
        $(domSearch, domParent).each(function(k, tableElement) {
            var listForm = {
                table: $(tableElement)
            }
            // Fetch select hidden field.
            // This field should contain the selected row id, it is used by the server.
            listForm.selectField = $('input#' + listForm.table.data('selection-field-id')); 

            // The selectables buttons used to validate the form should be next to the select field.
            listForm.buttons = $('.bxtr-autoload-datatables-selector', listForm.selectField.closest("form")); 
            //listForm.buttons = $('.bxtr-autoload-datatables-selector', listForm.selectField.parent().parent()); 
            //console.log(listForm.selectField, listForm.buttons, listForm.selectField.closest("form")); 
            // Selection event registration.
            // This has to be done before datatables creation, as datatables remove 
            // out of range rows.
            var rows = jQuery('tbody > tr', listForm.table);

            // DataTables configuration.
            // jQuery.extends merge the two objects
            var datatablesConfig = jQuery.extend(datatablesOptions, { 
                language: config.lang.datatables,
            });
            console.log(datatablesConfig)
            listForm.table.dataTable(datatablesConfig);

            rows.click(function() {
                // Unselect all rows.
                rows.removeClass('selected');

                // Select the clicked one.
                $(this).addClass('selected');
                
                // Store selected row for later use (buttons)
                listForm.selectField.prop('value', $(this).data('row-id'));
                //listForm.selectedRow = this;
                

                // write the current id to the seleted_id hidden field

                // Undirectly-related action : enable the stored buttons.
                listForm.buttons.prop('disabled', false);
            });

           listForm.buttons.prop('disabled', true) 
        });
    };
    autoLoadTables('table.bxtr-autoload-datatables');
    autoLoadTables('table.bxtr-autoload-checklist', null, {
        columns: [
                { "width": "20px", "orderDataType": "dom-checkbox" },
                null,
        ]
    });
});
